package logic

import (
	"context"
	"errors"
	"houses_srv/models"

	"houses_srv/houses"
	"houses_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
	return &UpdateUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateUserLogic) UpdateUser(in *houses.UpdateUserRequest) (*houses.UpdateUserResponse, error) {
	// todo: add your logic here and delete this line

	var h models.House
	h.UserId = int(in.UserId)
	err := h.ShowHouseByUserId()
	if err != nil {
		return nil, err
	}
	if h.ID != 0 {
		logx.Error("修改失败", errors.New("用户有发布的房源信息，不允许修改个人信息"))
		return nil, errors.New("用户有发布的房源信息，不允许修改个人信息")
	}

	var u models.User
	u.Nick = in.Nick
	err = u.UpdateUser()
	if err != nil {
		return nil, err
	}

	return &houses.UpdateUserResponse{UserId: int64(u.ID)}, nil
}
